package com.example.zzl.adapter.HomeFragment;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.example.zzl.R;
import com.example.zzl.adapter.BaseRecyclerViewAdapter;
import com.example.zzl.custom.MyTag;
import com.example.zzl.databinding.InfoBrandIntroduceBinding;
import com.example.zzl.databinding.InfoCommodityCommentBinding;
import com.example.zzl.databinding.InfoCommodityContentBinding;
import com.example.zzl.databinding.InfoEditCommentBinding;
import com.example.zzl.model.bean.Info;
import com.example.zzl.utils.specification.VolleySingleton;

import java.util.List;

/**
 * Created by ligg on 2017/2/13.
 */

public class InfoAdapter extends BaseRecyclerViewAdapter<Object, InfoAdapter.ViewHolder> {

    private int[] type;
    private Info.GetInfoDataBean getInfoDataBean;
    private Info.GetInfoReviewBean getInfoReviewBean;
    private Info.GetInfoBrandDataBean getInfoBrandDataBean;

    public InfoAdapter(Context context, List<Object> list) {
        super(context, list);
    }

    @Override
    public int getItemCount() {
        if (type != null) {
            return type.length;
        }
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        return type[position];
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 0:
                InfoEditCommentBinding editDb = DataBindingUtil.inflate(inflater, R.layout.info_edit_comment, parent, false);
                ViewHolder viewHolder = new ViewHolder(editDb);
                viewHolder.editDb = editDb;
                return viewHolder;

            case 1:
                InfoCommodityContentBinding contentDb = DataBindingUtil.inflate(inflater, R.layout.info_commodity_content, parent, false);
                ViewHolder contentviewHolder = new ViewHolder(contentDb);
                contentviewHolder.contentDb = contentDb;
                return contentviewHolder;

            case 2:
                InfoCommodityCommentBinding commentDb = DataBindingUtil.inflate(inflater, R.layout.info_commodity_comment, parent, false);
                ViewHolder commentVh = new ViewHolder(commentDb);
                commentVh.commentDb = commentDb;
                return commentVh;

            case 3:
                InfoBrandIntroduceBinding brandDb = DataBindingUtil.inflate(inflater, R.layout.info_brand_introduce, parent, false);
                ViewHolder brandVh = new ViewHolder(brandDb);
                brandVh.brandDb = brandDb;
                return brandVh;

            default:
                break;
        }
        return new ViewHolder(new TextView(context));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        switch (getItemViewType(position)) {
            case 0:
                holder.editDb.setResult(getInfoDataBean.result);
                break;

            case 1:
                holder.contentDb.setResult(getInfoDataBean.result);
                break;

            case 2:
                if (getInfoReviewBean.result != null) {
                    InfoLvAdapter infoLvAdapter = new InfoLvAdapter(context, getInfoReviewBean.result);
                    holder.commentDb.lv.setAdapter(infoLvAdapter);
                }
                break;

            case 3:
                holder.brandDb.setIntroduce(getInfoBrandDataBean);
                final ImageView iv = holder.brandDb.brandOneIv;
                iv.setTag(new MyTag(getInfoBrandDataBean.result.strBrandLogo, position));
                VolleySingleton.volleySingleton.getImageLoader().get(getInfoBrandDataBean.result.strBrandLogo, new ImageLoader.ImageListener() {
                    @Override
                    public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                        if (((MyTag) iv.getTag()).tag.equals(response.getRequestUrl())) {
                            iv.setImageBitmap(response.getBitmap());
                        }
                    }

                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
                break;
            default:
                break;
        }

        holder.db.executePendingBindings();
    }



    public void initData(Info info) {
        int num = 2;
        getInfoDataBean = info.GetInfoData;
        getInfoReviewBean = info.GetInfoReview;
        String strEditorRemark = info.GetInfoData.result.strEditorRemark;
        if (!strEditorRemark.equals("") && strEditorRemark != null) {
            num++;
            if (info.GetInfoBrandData.result != null) {
                getInfoBrandDataBean = info.GetInfoBrandData;
                num++;
            }
            type = new int[num];
            for (int i = 0; i < num; i++) {
                type[i] = i;
            }
        } else {
            type = new int[2];
            type[0] = 1;
            type[1] = 2;
        }

//        getInfoDataBean = ((Info.GetInfoDataBean) list.get(0));
       /* switch (list.size()) {
            case 2:
                Object o = list.get(1);
                if (o instanceof Info.GetInfoReviewBean) {
                    getInfoReviewBean = ((Info.GetInfoReviewBean) o);
                } else if (o instanceof Info.GetInfoBrandDataBean) {
                    getInfoBrandDataBean = ((Info.GetInfoBrandDataBean) o);
                }
                break;

            case 3:
                getInfoReviewBean = ((Info.GetInfoReviewBean) list.get(1));
                getInfoBrandDataBean = ((Info.GetInfoBrandDataBean) list.get(2));
                break;

            default:
                break;
        }*/
    }


    class ViewHolder extends RecyclerView.ViewHolder {

        public InfoEditCommentBinding editDb;
        public InfoBrandIntroduceBinding brandDb;
        public InfoCommodityContentBinding contentDb;
        public InfoCommodityCommentBinding commentDb;
        public ViewDataBinding db;

        private ViewHolder(View itemView) {
            super(itemView);
        }

        public ViewHolder(ViewDataBinding db) {
            super(db.getRoot());
            this.db = db;
        }
    }

    class Test extends ViewHolder {

        public Test(ViewDataBinding db) {
            super(db);
        }
    }


}
